<template>
	<view class="container">
		<mg-cell @tab="" last="1" isr='1' btt='原密码' bttc="c3 f30" :ft="form.oldPassword" ac="cd" />
		
		<view class="form-wrapper">
			<!-- <view class="form-title">修改密码</view> -->
			
			<view class="input-group">
				<view class="input-item">
					<text class="input-label">旧密码</text>
					<view class="input-content">
						<uni-icons type="locked" size="18" color="#999" class="input-icon"></uni-icons>
						<input 
							class="input-field" 
							type="password" 
							v-model="form.oldPassword" 
							placeholder="请输入旧密码"
							placeholder-class="placeholder"
						/>
					</view>
					<text class="error-msg" v-if="errors.oldPassword">{{ errors.oldPassword }}</text>
				</view>

				<view class="input-item">
					<text class="input-label">新密码</text>
					<view class="input-content">
						<uni-icons type="locked" size="18" color="#999" class="input-icon"></uni-icons>
						<input 
							class="input-field" 
							type="password" 
							v-model="form.newPassword" 
							placeholder="6-20位字母数字组合"
							placeholder-class="placeholder"
						/>
					</view>
					<text class="error-msg" v-if="errors.newPassword">{{ errors.newPassword }}</text>
				</view>

				<view class="input-item">
					<text class="input-label">确认密码</text>
					<view class="input-content">
						<uni-icons type="locked" size="18" color="#999" class="input-icon"></uni-icons>
						<input 
							class="input-field" 
							type="password" 
							v-model="form.confirmPassword" 
							placeholder="请再次输入新密码"
							placeholder-class="placeholder"
						/>
					</view>
					<text class="error-msg" v-if="errors.confirmPassword">{{ errors.confirmPassword }}</text>
				</view>
			</view>

			<my-button 
				margin-top="60" 
				background="#fe9367" 
				color="#fff"
				radius="50"
				@confirm="handleSubmit"
			>
				确认修改
			</my-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					oldPassword: '',
					newPassword: '',
					confirmPassword: ''
				},
				errors: {
					oldPassword: '',
					newPassword: '',
					confirmPassword: ''
				}
			}
		},
		methods: {
			validateForm() {
				let isValid = true
				this.errors = { oldPassword: '', newPassword: '', confirmPassword: '' }
				
				if (!this.form.oldPassword.trim()) {
					this.errors.oldPassword = '旧密码不能为空'
					isValid = false
				}
				
				const pwdRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,20}$/
				if (!pwdRegex.test(this.form.newPassword)) {
					this.errors.newPassword = '需包含字母和数字且6-20位'
					isValid = false
				}
				
				if (this.form.newPassword !== this.form.confirmPassword) {
					this.errors.confirmPassword = '两次输入的密码不一致'
					isValid = false
				}
				
				return isValid
			},
			async handleSubmit() {
				if (!this.validateForm()) return
				
				try {
					const res = await this.util.request({
						url: this.api.updatePassword,
						method: 'POST',
						data: {
							oldPassword: this.form.oldPassword,
							newPassword: this.form.newPassword
						}
					})
					
					uni.showToast({ 
						title: '密码修改成功',
						icon: 'success',
						duration: 1500
					})
					
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				} catch (e) {
					uni.showToast({
						title: '修改失败，请稍后重试',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background: #f5f6f8;
		min-height: 100vh;
		// padding: 40rpx;
		padding: 10rpx 20rpx ;
	}
	
	.form-wrapper {
		background: #fff;
		border-radius: 24rpx;
		padding: 50rpx 40rpx;
		box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.05);
	}
	
	.form-title {
		font-size: 40rpx;
		color: #333;
		font-weight: 600;
		text-align: center;
		margin-bottom: 60rpx;
	}
	
	.input-group {
		margin-bottom: 60rpx;
	}
	
	.input-item {
		margin-bottom: 50rpx;
		position: relative;
		
		&:last-child {
			margin-bottom: 0;
		}
	}
	
	.input-label {
		font-size: 28rpx;
		color: #666;
		display: block;
		margin-bottom: 20rpx;
	}
	
	.input-content {
		display: flex;
		align-items: center;
		height: 88rpx;
		background: #f8f8f8;
		border-radius: 16rpx;
		padding: 0 30rpx;
		border: 2rpx solid #eee;
		transition: all 0.3s;
		
		&:focus-within {
			border-color: #fe9367;
			background: #fff;
		}
	}
	
	.input-icon {
		margin-right: 20rpx;
	}
	
	.input-field {
		flex: 1;
		height: 100%;
		font-size: 30rpx;
		color: #333;
	}
	
	.placeholder {
		color: #ccc;
		font-size: 28rpx;
	}
	
	.error-msg {
		display: block;
		font-size: 24rpx;
		color: #ff4444;
		margin-top: 16rpx;
		padding-left: 10rpx;
	}
</style>